//
//  MCInstrumentVc.swift
//  MCBodyBuildingPro
//
//  Created by Renlvshijie on 2023/5/5.
//

import UIKit
import SnapKit
import JXSegmentedView
import SwiftyJSON
import Toast_Swift
import QMUIKit
import MJRefresh
import Kingfisher

class MCInstrumentVc: MCBaseVc {
    
    let instrumentIdentifier:String = "instrumentIdentifier"
    
    
    var instrumentCollectionV:UICollectionView?
    
    var count = 1
    
    var dataSource:Array = [MineRecord?]()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

     
        let instrumentCollLayout = UICollectionViewFlowLayout.init()
        
        instrumentCollectionV = UICollectionView.init(frame: CGRect.zero, collectionViewLayout: instrumentCollLayout)
        instrumentCollectionV?.delegate = self
        instrumentCollectionV?.dataSource = self
        instrumentCollectionV?.backgroundColor = UIColor.white
        instrumentCollectionV?.register(MCFitnessCollectionCell.self, forCellWithReuseIdentifier: instrumentIdentifier)
        view.addSubview(instrumentCollectionV!)
        instrumentCollectionV?.snp.makeConstraints { make in
            make.edges.equalTo(view).inset(UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0))
        }
        
        self.instrumentCollectionV?.mj_header = MJRefreshNormalHeader.init(refreshingBlock: {
            self.count = 1
            self.getInstrumentListData(current: self.count)
        })
        
        self.instrumentCollectionV?.mj_header?.beginRefreshing()
        
        self.instrumentCollectionV?.mj_footer = MJRefreshFooter.init(refreshingBlock: {
            self.getInstrumentListData(current: self.count)
        })
        
    }
    
    
    //健身机械
    func getInstrumentListData(current:Int?) -> Void {
        NetWorkRequest(MCMyCollectApi.myInstrumentList(current: current)) { ResponseModel in
            self.instrumentCollectionV?.mj_header?.endRefreshing()
            self.instrumentCollectionV?.mj_footer?.endRefreshing()
            let intrumentListModel = MineCollectIntrumentList(JSONString: ResponseModel.dataString)
            if current == 1 {
                self.dataSource.removeAll()
                self.dataSource.append(contentsOf: intrumentListModel?.records ?? [])
            }else{
                self.count += 1
                self.dataSource.append(contentsOf: intrumentListModel?.records ?? [])
            }
            self.instrumentCollectionV?.reloadData()
            
        } failureCallback: { ResponseModel in
            self.instrumentCollectionV?.mj_header?.endRefreshing()
            self.instrumentCollectionV?.mj_footer?.endRefreshing()
            self.view.makeToast("网络失败",position: .center)
        }
    }
     

}

extension MCInstrumentVc:UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout {
    
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 10
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 10
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: (UIScreen.main.bounds.size.width - 30) / 2, height: 140)
    }
    
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.dataSource.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell:MCFitnessCollectionCell = collectionView.dequeueReusableCell(withReuseIdentifier: instrumentIdentifier, for: indexPath) as! MCFitnessCollectionCell
       
        let instrumentRecordList =  self.dataSource[indexPath.row]
        
        cell.titleInfoLabel.text = instrumentRecordList?.zhName ?? ""
        cell.icHeaderImageView.kf.setImage(with: URL(string: instrumentRecordList?.previewPic ?? ""))
        
        return cell
    }
    
    
}

extension MCInstrumentVc:JXSegmentedListContainerViewListDelegate {
    func listView() -> UIView {
        return view
    }
}
